<template>
  <nav class="navbar" :class="{ 'scrolled': isScrolled }">
    <div class="navbar-container">
      <router-link class="navbar-brand" to="/">
        <h3 class="logo-text">考研数据帝</h3>
      </router-link>

      <!-- 移动端菜单按钮 -->
      <button
          class="mobile-menu-button"
          @click="toggleMobileMenu"
          aria-label="Toggle navigation"
      >
        <span class="menu-icon" :class="{ 'open': isMobileMenuOpen }"></span>
      </button>

      <!-- 导航菜单 -->
      <div class="nav-menu" :class="{ 'open': isMobileMenuOpen }">
        <ul class="nav-list">
          <li
              class="nav-item"
              v-for="item in navItems"
              :key="item.name"
              @click="closeMobileMenu"
          >
            <router-link
                class="nav-link"
                :to="item.path"
                :class="{ 'active': activeRoute === item.path }"
            >
              {{ item.name }}
              <span class="link-underline"></span>
            </router-link>
          </li>
          <li class="nav-item" @click="closeMobileMenu">
            <router-link class="nav-button" to="/login">登录/注册</router-link>
          </li>
        </ul>
      </div>
    </div>
  </nav>
</template>

<script>
export default {
  name: 'NavBar',
  data() {
    return {
      isScrolled: false,
      isMobileMenuOpen: false,
      activeRoute: '/',
      navItems: [
        { name: '首页', path: '/#home' },
        { name: '数据统计', path: '/#stats' },
        { name: '核心功能', path: '/#features' },
        { name: '关于我们', path: '/about' },
        { name: '联系我们', path: '/contact' }
      ]
    }
  },
  methods: {
    handleScroll() {
      this.isScrolled = window.scrollY > 50
    },
    toggleMobileMenu() {
      this.isMobileMenuOpen = !this.isMobileMenuOpen
    },
    closeMobileMenu() {
      this.isMobileMenuOpen = false
    },
    setActiveRoute() {
      this.activeRoute = this.$route.path
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
    this.setActiveRoute()
    this.$watch('$route', this.setActiveRoute)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style>
/* 基础变量 */
:root {
  --primary-color: #3498db;
  --secondary-color: #2ecc71;
  --dark-color: #2c3e50;
  --light-color: #ecf0f1;
  --white: #ffffff;
  --black: #000000;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

/* 导航栏基础样式 */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: var(--white);
  box-shadow: var(--shadow);
  z-index: 1000;
  transition: var(--transition);
  padding: 1rem 0;
}

.navbar.scrolled {
  padding: 0.5rem 0;
  background-color: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
}

.navbar-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
}

/* Logo样式 */
.logo-text {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary-color);
  background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  transition: var(--transition);
}

.navbar.scrolled .logo-text {
  font-size: 1.3rem;
}

/* 导航菜单 */
.nav-menu {
  display: flex;
}

.nav-list {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  align-items: center;
}

.nav-item {
  margin-left: 1.5rem;
  position: relative;
}

.nav-link {
  position: relative;
  display: inline-block;
  padding: 0.5rem 0;
  color: var(--dark-color);
  font-weight: 500;
  text-decoration: none;
  transition: var(--transition);
}

.nav-link:hover {
  color: var(--primary-color);
}

.nav-link.active {
  color: var(--primary-color);
}

.link-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
  transition: var(--transition);
}

.nav-link:hover .link-underline,
.nav-link.active .link-underline {
  width: 100%;
}

/* 登录按钮 */
.nav-button {
  display: inline-block;
  padding: 0.5rem 1.5rem;
  background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
  color: var(--white);
  border-radius: 50px;
  font-weight: 500;
  text-decoration: none;
  transition: var(--transition);
  box-shadow: 0 2px 10px rgba(52, 152, 219, 0.3);
}

.nav-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(52, 152, 219, 0.4);
}

/* 移动端菜单按钮 */
.mobile-menu-button {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
  z-index: 1001;
}

.menu-icon {
  position: relative;
  display: block;
  width: 24px;
  height: 2px;
  background-color: var(--dark-color);
  transition: var(--transition);
}

.menu-icon::before,
.menu-icon::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: var(--dark-color);
  transition: var(--transition);
}

.menu-icon::before {
  top: -6px;
}

.menu-icon::after {
  bottom: -6px;
}

.menu-icon.open {
  background-color: transparent;
}

.menu-icon.open::before {
  top: 0;
  transform: rotate(45deg);
  background-color: var(--primary-color);
}

.menu-icon.open::after {
  bottom: 0;
  transform: rotate(-45deg);
  background-color: var(--primary-color);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar-container {
    padding: 0 1rem;
  }

  .mobile-menu-button {
    display: block;
  }

  .nav-menu {
    position: fixed;
    top: 0;
    right: -100%;
    width: 70%;
    height: 100vh;
    background-color: var(--white);
    flex-direction: column;
    justify-content: center;
    transition: var(--transition);
    box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
  }

  .nav-menu.open {
    right: 0;
  }

  .nav-list {
    flex-direction: column;
    align-items: flex-start;
    padding: 2rem;
  }

  .nav-item {
    margin: 1rem 0;
  }

  .nav-link {
    font-size: 1.2rem;
  }

  .nav-button {
    margin-top: 1rem;
  }
}
</style>